<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoLoad(){
        $autoloader = new Zend_Application_Module_Autoloader( array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH,
        ));
        
        return $autoloader;
    }
    
    protected function _initDb(){
        $dbOptionds = $this->getOption('resources');
        $dbOptionds = $dbOptionds['db'];
        
        // Set up db
        $db = Zend_Db::factory($dbOptionds['adapter'], $dbOptionds['params']);
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        
        Zend_Registry::set('connectDB', $db);
        // Khi thiet la che do nay model moi co the su dung duoc
        Zend_Db_Table::setDefaultAdapter($db);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        // return it, so that it can be stored in bootstrap
        return $db;
    }
    
    //	Khoi tao mail phc v gui mail

	protected function _initMail() {
		$mailOption = $this->getOption('resources');
		$mailOption = $mailOption['mail'];
		$host = $mailOption['host'];
		$mailOption = $mailOption['info'];
    	
    	$transport = new Zend_Mail_Transport_Smtp($host, $mailOption);
        //Zend_Debug::dump($transport);die;
    	Zend_Registry::set('mailTransport',$transport);
    	Zend_Mail::setDefaultTransport ( $transport );
        Zend_Mail::setDefaultFrom(EMAIL_ADMIN);
	}
    
    // Them file cau hinh cac hang so su dung trong project
    protected function _initSetConstants(){
        $config = parse_ini_file(APPLICATION_PATH . '/configs/constant.ini');
        foreach($config as $key=>$value){
            // neu chua dinh nghia thi moi dinh nghia lai
            if(!defined($key)){
                define($key, $value);
            }
        }
    }
    
    protected function _initFrontController(){
        $front = Zend_Controller_Front::getInstance();       
        $front->addModuleDirectory(APPLICATION_PATH . "/modules");
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'thietlap');
        $router = new Zend_Controller_Router_Rewrite();
        $router->addConfig($config, 'routes');
        $front->setRouter($router);
        return $front;
    }
    
     protected function _initNavigation(){
	    $this->bootstrap('layout');
	    $layout = $this->getResource('layout');
	    $view = $layout->getView();
	    $config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
            
	    $navigation = new Zend_Navigation($config);
	    $view->navigation($navigation);
	}
}